/*
 * OknoLogowania.java
 *
 * Created on 24 maj 2009, 14:18
 */

package BasaSobol;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author  bąsa
 */
public class OknoLogowania extends javax.swing.JFrame implements ActionListener {
    private boolean trwaLogowanie = false;
    
    /** Creates new form OknoLogowania */
    public OknoLogowania() {
        initComponents();
        logowaniePrzycisk.addActionListener(this);
        nowaSiec.addActionListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        logowaniePole = new javax.swing.JTextField();
        logowaniePrzycisk = new javax.swing.JButton();
        nowaSiec = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Login:");

        logowaniePrzycisk.setText("Logowanie do systemu MIAU MIAU");

        nowaSiec.setText("Nowa sieć");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(nowaSiec, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                    .addComponent(logowaniePrzycisk, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(logowaniePole, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(logowaniePole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(9, 9, 9)
                .addComponent(logowaniePrzycisk)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nowaSiec)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void mainniema(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OknoLogowania().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField logowaniePole;
    private javax.swing.JButton logowaniePrzycisk;
    private javax.swing.JButton nowaSiec;
    // End of variables declaration//GEN-END:variables

    /**
     * Funkcja wywoływana, aby przerwać proces logowania do sieci
     */
    public void zresetujLogowanie() {
        trwaLogowanie = false;
        logowaniePole.setText("");
    }

    /**
     * Funkcja wywoływana aby zalogować się do istniejącej sieci.
     * Program zostaje ustawiony w tryb oczekiwania na pakiety REG /ACK
     */
    private void kliknietoPrzycisk() {
        if(!trwaLogowanie) {
            trwaLogowanie = true;
            tinsiec.MessageInfo msg = new tinsiec.MessageInfo(modulXML.wiadomoscONowymKliencie(logowaniePole.getText()), "");
            System.out.println(msg.getContent());
            Main.mojLogin = new String(logowaniePole.getText());
            Main.mojStatus = new String("Dostepny");
            Main.wiadomosciWychodzaceMulticast.add(msg);
            logowaniePole.setText("trwa logowanie..");
        } else {
            System.out.println("juz trwa logowanie");
        }
    }

    /**
     * Funkcja wywoływana w celu utworzenia nowej sieci
     * Program nie oczekuje na pakiety REG / ACK
     */
    private void tworzNowaSiec() {
        if(!trwaLogowanie) {
            Main.oknoGlowne = new OknoGlowne();
            Main.oknoGlowne.setVisible(true);
            Main.mojLogin = new String(logowaniePole.getText());
            Main.mojStatus = new String("Dostepny");
            this.setVisible(false);
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Logowanie do systemu MIAU MIAU")) {
            kliknietoPrzycisk();
        }
        
        if(e.getActionCommand().equals("Nowa sieć")) {
            tworzNowaSiec();
        }
    }
    
}
